<?php
class UserRoleModel
{
    private $userRoleKeyPrefix  	=   'URLO/';
    private $guestRoleKeyPrefix     =   'GRLO/';
    private $check = false;

    /**
     * 初始化用户角色
     * @param $type
     * @param $info
     * @return bool|int|resource
     */
    public function initRole($type, $info)
    {
        $result = 0;
        $data = $this->initRoleDataOfMysql($info);

        switch ($type){
            case 'user':
                $this->checkInitParam($data['uid']);
                $result = $this->initRoleToMysql($this->userRoleKeyPrefix. 0, $data);
                break;
            case 'guest':
                $this->checkInitParam($data['uuid']);
                $result = $this->initRoleToMysql($this->guestRoleKeyPrefix. $data['uuid'], $data);
                break;
        }

        return $result;
    }

    /**
     * 获取角色信息
     * @param $type
     * @param $uid
     * @param $uuid
     * @return array|bool
     */
    public function getRoleInfo($type, $uid, $uuid)
    {
        $result = array();
        switch ($type){
            case 'user':
                $this->checkInitParam($uid);
                $result = $this->getRoleFromMysql($this->userRoleKeyPrefix. 0, array('uid' => $uid));
                break;
            case 'guest':
                $this->checkInitParam($uuid);
                $result = $this->getRoleFromMysql($this->guestRoleKeyPrefix. $uuid, array('uuid' => $uuid));
                break;
        }

        return $result;
    }

    /**
     * 更新角色信息
     * @param $type
     * @param $info
     * @return bool|int|resource
     */
    public function updateRoleInfo($type, $info)
    {
        $result = 0;
        $uid  = intval($info['uid']);
        $uuid = strval($info['uuid']);

        switch ($type){
            case 'user':
                $this->checkInitParam($uid);
                $result = $this->setRoleToMysql($this->userRoleKeyPrefix. 0, array('uid' => $uid), $info);
                break;
            case 'guest':
                $this->checkInitParam($uuid);
                $result = $this->setRoleToMysql($this->guestRoleKeyPrefix. $uuid, array('uuid' => $uuid), $info);
                break;
        }

        return $result;
    }

    /**
     * 更新数据
     * @param $key
     * @param $where
     * @param $data
     * @return bool|int|resource
     */
    private function setRoleToMysql($key, $where, $data)
    {
        if (empty($this->check) || empty($key) || empty($where) || empty($data))
        {
            return false;
        }

        $data = $this->updateRoleDataOfMysql($data);
        return Storage::setConditionData($key, $data, $where);
    }

    /**
     * 组装更新数据
     * @param $info
     * @return array
     */
    private function updateRoleDataOfMysql($info)
    {
        $data = array( 'update_at' =>  AC::SERVER_TIME );
        isset($info['gender'])      && $data['gender']      = intval($info['gender']);
        isset($info['role'])        && $data['role']        = intval($info['role']);
        isset($info['schedules'])   &&  $data['schedules']   = strval($info['schedules']);
        isset($info['settings'])   &&  $data['settings']   = strval($info['settings']);

        if (count($data) < 2) $data = array();

        return $data;
    }

    /**
     * 从数据库获取角色信息
     * @param $key
     * @param $where
     * @return Array|bool
     */
    private function getRoleFromMysql($key, $where)
    {
        if (empty($this->check) || empty($key) || empty($where))
        {
            return false;
        }

        return Storage::getConditionData($key, $where);
    }

    /**
     * 组装创建角色进数据库的数据
     * @param $info
     * @return array
     */
    private function initRoleDataOfMysql($info)
    {
        return array(
            'uid'       =>  intval($info['uid']),
            'uuid'      =>  strval($info['uuid']),
            'gender'    =>  intval($info['gender']),
            'role'      =>  intval($info['role']),
            'schedules' =>  strval($info['schedules']),
            'settings'  =>  strval($info['settings']),
            'update_at' =>  AC::SERVER_TIME,
            'create_at' =>  AC::SERVER_TIME,
        );
    }

    /**
     * 判断必要参数
     * @param $id
     */
    private function checkInitParam($id)
    {
        if ($id)
        {
            $this->check = true;
        }
    }

    /**
     * 初始化数据
     * @param $key
     * @param $data
     * @return bool|int|resource
     */
    private function initRoleToMysql($key, $data)
    {
        if (empty($this->check)) return false;

        return Storage::addData($key, $data);
    }

}